package com.zhongxiangspace.controller.admin;

import com.github.pagehelper.Page;
import com.zhongxiangspace.entity.*;
import com.zhongxiangspace.service.*;
import com.zhongxiangspace.tools.FreeMarkerViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成静态页面
 */
@Controller
public class FreeMarkerViewHtmlController {
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private StationBranchService stationBranchService;


    @GetMapping("/freemarker/create/newsdetail_a.do")
    public String newsdetail_a(Integer id, Model model) {
        Map<String, Object> map = new HashMap<>();
        Articles articles = this.articleService.queryOne(id);
        model.addAttribute("articles", articles);//news 内容
        ArticleType articleType = this.articleService.queryTypeOne(articles.getArticle_type_id());
        model.addAttribute("articleType", articleType);//文章类型
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        model.addAttribute("officeBuildings", officeBuildingsList);
        //热门文章
        QueryAnalyzer newsQueryAnalyzer = new QueryAnalyzer();
        newsQueryAnalyzer.setShowCode(1);
        newsQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> articleHotList = this.articleService.articlesAll(newsQueryAnalyzer);
        model.addAttribute("articleHotList", articleHotList);
        QueryAnalyzer newQueryAnalyzer = new QueryAnalyzer();
        newQueryAnalyzer.setShowCode(1);
        List<Map<String, Object>> articlenewList = this.articleService.articlesAll(newQueryAnalyzer);
        model.addAttribute("articlenewList", articlenewList);
        //上一篇文章
        Articles articlesUP = this.articleService.queryOneUP(id);
        if(articlesUP!=null){
            System.out.println();
            model.addAttribute("articlesUP", articlesUP);
        }else{
            articlesUP.setTitle("已经是第一篇文章了");
            articlesUP.setArticle_id(1);
            model.addAttribute("articlesUP", articlesUP);
        }
       //下一篇文章
        Articles articlesDOWN  = this.articleService.queryOneDOWN(id);
        if(articlesDOWN!=null){
            model.addAttribute("articlesDOWN", articlesDOWN);
        }else{
            articlesDOWN.setTitle("已经是最后一篇文章了");
            articlesDOWN.setArticle_id(1);
            model.addAttribute("articlesDOWN", articlesDOWN);
        }
        return "html/pc/newsdetail";
    }


    @ResponseBody
    @GetMapping("/freemarker/create/newsdetail.do")
    public Object newsdetail(Integer id, Model model) {
        Map<String, Object> map = new HashMap<>();
        Articles articles = this.articleService.queryOne(id);
        map.put("articles", articles);
        ArticleType articleType = this.articleService.queryTypeOne(articles.getArticle_type_id());
        map.put("articleType", articleType);
        model.addAttribute("data", map);
        return map;
    }

    @GetMapping("/freemarker/create/index.do")
    public Object index() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", "第一次触发!");
        FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
        freemarkerHtmlConfiguration.setTemplateName("index.ftl");
        freemarkerHtmlConfiguration.setHtmlPath("D:\\zhongxiansgpace\\src\\main\\resources\\templates\\index.html");
        freemarkerHtmlConfiguration.setData(map);
        FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
        return "";
    }

    @GetMapping("/freemarker/create/housedetail.do")
    public String housedetail(Integer id, Model model) {
        Map<String, Object> map = new HashMap<>();
        House house = this.houseService.queryOne(id);
        map.put("house", house);
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(house.getBuilding_id());
        map.put("buildingsAttribute", buildingsAttribute);
        County county = this.countyService.queryOne(house.getCounty_id());
        map.put("county", county);
        County trade_area = this.countyService.queryOne(house.getTrade_area_id());
        map.put("trade_area", trade_area);
        OfficeBuildings office = this.officeBuildingService.queryOne(house.getBuilding_id());
        map.put("office", office);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        model.addAttribute("data", map);
        return "html/pc/housedetail";
    }


    @GetMapping("/freemarker/create/stationdetail.do")
    public String stationdetail(Integer id, Model model) {
        Map<String, Object> map = new HashMap<>();
        StationBranch stationBranch = this.stationBranchService.queryOne(id);
        model.addAttribute("stationBranch", stationBranch);
        County county = this.countyService.queryOne(stationBranch.getCounty_id());
        model.addAttribute("county", county);
        County trade_area = this.countyService.queryOne(stationBranch.getTrade_area_id());
        model.addAttribute("trade_area", trade_area);
        OfficeBuildings office = this.officeBuildingService.queryOne(stationBranch.getBuilding_id());
        model.addAttribute("office", office);
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(office.getBuilding_id());
        model.addAttribute("buildingsAttribute", buildingsAttribute);
        List<StationHouse> stationHouse = this.stationBranchService.queryStationHouse(id);
        String min = "800";
        String mxn = "2000";
        if (stationHouse.size() > 0) {
            min = stationHouse.get(0).getPrice();
            mxn = stationHouse.get(stationHouse.size() - 1).getPrice();

        }

        model.addAttribute("price_area", min + "-" + mxn);
        model.addAttribute("stationHouse", stationHouse);
        List<StationIcon> stationIconList = this.stationBranchService.queryIcon(null);
        model.addAttribute("stationIconList", stationIconList);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        model.addAttribute("officeBuildings", officeBuildingsList);
        return "html/pc/projectdetail";
    }

    @ResponseBody
    @GetMapping("/freemarker/create/stationdetail-a.do")
    public Object stationdetail_aa(Integer id, Model model) {
        Map<String, Object> map = new HashMap<>();

        StationBranch stationBranch = this.stationBranchService.queryOne(id);
        map.put("stationBranch", stationBranch);
        County county = this.countyService.queryOne(stationBranch.getCounty_id());
        map.put("county", county);
        County trade_area = this.countyService.queryOne(stationBranch.getTrade_area_id());
        map.put("trade_area", trade_area);
        OfficeBuildings office = this.officeBuildingService.queryOne(stationBranch.getBuilding_id());
        map.put("office", office);
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(office.getBuilding_id());
        map.put("buildingsAttribute", buildingsAttribute);
        List<StationHouse> stationHouse = this.stationBranchService.queryStationHouse(id);
        String min = "800";
        String mxn = "2000";
        if (stationHouse.size() > 0) {
            min = stationHouse.get(0).getArea();
            mxn = stationHouse.get(stationHouse.size() - 1).getArea();
        }
        map.put("price_area", min + "-" + mxn);
        map.put("stationHouse", stationHouse);
        List<StationIcon> stationIconList = this.stationBranchService.queryIcon(null);
        map.put("stationIconList", stationIconList);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        return map;
    }
}

